package IO流.对象流;

import org.junit.jupiter.api.Test;

import java.io.*;
@SuppressWarnings("all")
public class ObjectStream01 {

    @Test
    public void objectStream() throws IOException {
        String filePath = "src/main/java/IO流/对象流/1.txt";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));

        //序列化数据到文件
        //objectOutputStream.write(1000); //int -> Integer 自动装箱
//        objectOutputStream.writeBoolean(true);
//        objectOutputStream.writeChar('a');
//        objectOutputStream.writeUTF("北京欢迎你");
        objectOutputStream.writeObject(new A("小溪",12));
        objectOutputStream.close();

        //反序列化

    }

    @Test
    public void readObject() throws IOException, ClassNotFoundException {
        String filePath = "src/main/java/IO流/对象流/1.txt";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
//        System.out.println(objectInputStream.readInt());
//        System.out.println(objectInputStream.readBoolean());
//        System.out.println(objectInputStream.readChar());
//        System.out.println(objectInputStream.readUTF());
//        System.out.println(objectInputStream.readObject());
        Object obj = objectInputStream.readObject();
        A a = (A)obj;
        System.out.println(a.getAge());
        System.out.println(a.getName());
        objectInputStream.close();
    }
}

class A implements Serializable {
    private String name;
    private int age;

    public A(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
